package com.xingongchang.baolebei.protocol;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.external.activeandroid.Model;
import com.external.activeandroid.annotation.Column;
import com.external.activeandroid.annotation.Table;
@Table(name = "Fen")
public class Fen extends Model{
	@Column(name = "gid")
	  public String gid;
	  @Column(name = "gname")
	  public String gname;
	  @Column(name = "gimg")
	  public String gimg;
	  @Column(name = "gthumb")
	  public String gthumb;
	  public ArrayList<Fen> children = new ArrayList<Fen>();
	  public static Fen fromJson(JSONObject jsonObject) throws JSONException {
			if (null == jsonObject) {
				return null;
			}
			Fen localItem = new Fen();
			JSONArray subItemArray;
			localItem.gid = jsonObject.optString("id");
			localItem.gname = jsonObject.optString("name");
			localItem.gimg = jsonObject.optString("thumbImg");
			localItem.gthumb = jsonObject.optString("cat_desc");
			subItemArray = jsonObject.optJSONArray("children");
			if (null != subItemArray) {
				for (int i = 0; i < subItemArray.length(); i++) {
					JSONObject subItemObject = subItemArray.getJSONObject(i);
					Fen subItem = Fen.fromJson(subItemObject);
					localItem.children.add(subItem);
				}
			}
			return localItem;
		}

		public JSONObject toJson() throws JSONException {
			JSONObject localItemObject = new JSONObject();
			JSONArray itemJSONArray = new JSONArray();
			localItemObject.put("goods_id", gid);
		    localItemObject.put("goods_name", gname);
			localItemObject.put("goods_img", gimg);
			localItemObject.put("goods_thumb", gthumb);
			for (int i = 0; i < children.size(); i++) {
				Fen itemData = children.get(i);
				JSONObject itemJSONObject = itemData.toJson();
				itemJSONArray.put(itemJSONObject);
			}
			localItemObject.put("children", itemJSONArray);
			return localItemObject;
		}
}
